<?php

// /usr/local/bin/php -q /job_cron.php 

require("public_html/classes/class.phpmailer.php");
require("public_html/functions/settings.php");
require("public_html/functions/buildLinks.php");

$conn = mysql_connect( $dbHost, $dbUser, $dbPass ) or die("unable to connect to Database");
mysql_select_db($dbName);

$sql = "SELECT setting_value FROM settings WHERE setting_name = 'no_reply_email'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$settings=mysql_fetch_assoc($query);

// notify 7 day old accepted jobs

$seven_days_ago = date("Y-m-d", mktime(0,0,0,date("m"),date("d")-7,date("Y")));

$sql = "SELECT jobs.id, firstname, surname, email, trade, area 
	FROM jobs 
	LEFT JOIN users ON jobs.user_id = users.id 
	LEFT JOIN trades ON jobs.trade_id = trades.id 
	LEFT JOIN areas ON jobs.area_id = areas.id 
	WHERE jobs.date_accepted = '".$seven_days_ago."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());

while($rs=mysql_fetch_assoc($query)){

	$mail = new PHPMailer();
	$mail->IsSMTP(); // telling the class to use SMTP
	$mail->Host = "localhost"; // SMTP server
	$mail->From = $settings['setting_value'];
	$mail->FromName = "I Want A Tradesman";

	$mail->AddAddress($rs['email']);

	$mail->Subject = "Job acceptance";
	$mail->Body = "Dear ".$rs['firstname']." ".$rs['surname']."\r\n\r\nWe have noticed that you accepted a quote from a tradesmen on I Want A Tradesman 7 days ago. If the job has been completed, please login to your account and leave the tradesman feedback. Leaving your tradesman feedback, helps other users to understand the capabilities and quality of the tradesman's work.\r\n\r\nYou can access your account here: http://www.iwantatradesman.co.uk/my-account\r\n\r\nKind regards\r\n\r\nThe I Want A Tradesman Team\r\n\r\n\r\nThis email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.";

	$mail->Send();

}

// notify 14 day old accepted jobs

$fourteen_days_ago = date("Y-m-d", mktime(0,0,0,date("m"),date("d")-14,date("Y")));

$sql = "SELECT jobs.id, firstname, surname, email, trade, area 
	FROM jobs 
	LEFT JOIN users ON jobs.user_id = users.id 
	LEFT JOIN trades ON jobs.trade_id = trades.id 
	LEFT JOIN areas ON jobs.area_id = areas.id 
	WHERE jobs.date_accepted = '".$fourteen_days_ago."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());

while($rs=mysql_fetch_assoc($query)){

	$mail = new PHPMailer();
	$mail->IsSMTP(); // telling the class to use SMTP
	$mail->Host = "localhost"; // SMTP server
	$mail->From = $settings['setting_value'];
	$mail->FromName = "I Want A Tradesman";

	$mail->AddAddress($rs['email']);

	$mail->Subject = "Job acceptance";
	$mail->Body = "Dear ".$rs['firstname']." ".$rs['surname']."\r\n\r\nWe have noticed that you accepted a quote from a tradesmen on I Want A Tradesman 14 days ago. If the job has been completed, please login to your account and leave the tradesman feedback. Leaving your tradesman feedback, helps other users to understand the capabilities and quality of the tradesman's work.\r\n\r\nYou can access your account here: http://www.iwantatradesman.co.uk/my-account\r\n\r\nKind regards\r\n\r\nThe I Want A Tradesman Team\r\n\r\n\r\nThis email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.";

	$mail->Send();

}

// notify 21 day old accepted jobs

$twenty_one_days_ago = date("Y-m-d", mktime(0,0,0,date("m"),date("d")-21,date("Y")));

$sql = "SELECT jobs.id, firstname, surname, email, trade, area 
	FROM jobs 
	LEFT JOIN users ON jobs.user_id = users.id 
	LEFT JOIN trades ON jobs.trade_id = trades.id 
	LEFT JOIN areas ON jobs.area_id = areas.id 
	WHERE jobs.date_accepted = '".$twenty_one_days_ago."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());

while($rs=mysql_fetch_assoc($query)){

	$mail = new PHPMailer();
	$mail->IsSMTP(); // telling the class to use SMTP
	$mail->Host = "localhost"; // SMTP server
	$mail->From = $settings['setting_value'];
	$mail->FromName = "I Want A Tradesman";

	$mail->AddAddress($rs['email']);

	$mail->Subject = "Job acceptance";
	$mail->Body = "Dear ".$rs['firstname']." ".$rs['surname']."\r\n\r\nWe have noticed that you accepted a quote from a tradesmen on I Want A Tradesman 21 days ago. If the job has been completed, please login to your account and leave the tradesman feedback. Leaving your tradesman feedback, helps other users to understand the capabilities and quality of the tradesman's work.\r\n\r\nYou can access your account here: http://www.iwantatradesman.co.uk/my-account\r\n\r\nKind regards\r\n\r\nThe I Want A Tradesman Team\r\n\r\n\r\nThis email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.";

	$mail->Send();

}

// notify 2 week old open jobs

$fourteen_days_ago = date("Y-m-d", mktime(0,0,0,date("m"),date("d")-14,date("Y")));

$sql = "SELECT jobs.id, title, firstname, surname, email, trade, area 
	FROM jobs 
	LEFT JOIN users ON jobs.user_id = users.id 
	LEFT JOIN trades ON jobs.trade_id = trades.id 
	LEFT JOIN areas ON jobs.area_id = areas.id 
	WHERE jobs.date_added = '".$fourteen_days_ago."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());

while($rs=mysql_fetch_assoc($query)){

$job_link = build_job_link($rs['trade'],$rs['area'],$rs['id']);

	$mail = new PHPMailer();
	$mail->IsSMTP(); // telling the class to use SMTP
	$mail->Host = "localhost"; // SMTP server
	$mail->From = $settings['setting_value'];
	$mail->FromName = "I Want A Tradesman";

	$mail->AddAddress($rs['email']);

	$mail->Subject = "Job notification";
	$mail->Body = "Dear ".$rs['firstname']." ".$rs['surname']."\r\n\r\nThis email is regarding your job post: ".$rs['title']."\r\n\r\nWe have noticed that you have not yet accepted a quote for your job on I Want A Tradesman. You can view and accept quotes by visiting your job here: http://www.iwantatradesman.co.uk".$job_link."\r\n\r\nAlternatively, if you no longer need a quotation, please cancel the job through your account. You can access your account here: http://www.iwantatradesman.co.uk/my-account\r\n\r\nKind regards\r\n\r\nThe I Want A Tradesman Team\r\n\r\n\r\nThis email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.";

	$mail->Send();

}

// notify 4 week old open jobs

$twenty_eight_days_ago = date("Y-m-d", mktime(0,0,0,date("m"),date("d")-28,date("Y")));

$sql = "SELECT jobs.id, title, firstname, surname, email, trade, area 
	FROM jobs 
	LEFT JOIN users ON jobs.user_id = users.id 
	LEFT JOIN trades ON jobs.trade_id = trades.id 
	LEFT JOIN areas ON jobs.area_id = areas.id 
	WHERE jobs.date_added = '".$twenty_eight_days_ago."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());

while($rs=mysql_fetch_assoc($query)){

$job_link = build_job_link($rs['trade'],$rs['area'],$rs['id']);

	$mail = new PHPMailer();
	$mail->IsSMTP(); // telling the class to use SMTP
	$mail->Host = "localhost"; // SMTP server
	$mail->From = $settings['setting_value'];
	$mail->FromName = "I Want A Tradesman";

	$mail->AddAddress($rs['email']);

	$mail->Subject = "Job notification";
	$mail->Body = "Dear ".$rs['firstname']." ".$rs['surname']."\r\n\r\nThis email is regarding your job post: ".$rs['title']."\r\n\r\nWe have noticed that you have not yet accepted a quote for your job on I Want A Tradesman. You can view and accept quotes by visiting your job here: http://www.iwantatradesman.co.uk".$job_link."\r\n\r\nAlternatively, if you no longer need a quotation, please cancel the job through your account. You can access your account here: http://www.iwantatradesman.co.uk/my-account\r\n\r\nKind regards\r\n\r\nThe I Want A Tradesman Team\r\n\r\n\r\nThis email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.";

	$mail->Send();

}

// close 6 week old open jobs

$fourty_two_days_ago = date("Y-m-d", mktime(0,0,0,date("m"),date("d")-42,date("Y")));

$sql = "SELECT jobs.id, title, firstname, surname, email, trade, area 
	FROM jobs 
	LEFT JOIN users ON jobs.user_id = users.id 
	LEFT JOIN trades ON jobs.trade_id = trades.id 
	LEFT JOIN areas ON jobs.area_id = areas.id 
	WHERE jobs.date_added = '".$fourty_two_days_ago."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());

while($rs=mysql_fetch_assoc($query)){

	$mail = new PHPMailer();
	$mail->IsSMTP(); // telling the class to use SMTP
	$mail->Host = "localhost"; // SMTP server
	$mail->From = $settings['setting_value'];
	$mail->FromName = "I Want A Tradesman";

	$mail->AddAddress($rs['email']);

	$mail->Subject = "Job cancellation";
	$mail->Body = "Dear ".$rs['firstname']." ".$rs['surname']."\r\n\r\nThis email is regarding your job post: ".$rs['title']."\r\n\r\nWe have noticed that it has been 6 weeks since you posted your job and you have not yet accepted a quote. We have therefore cancelled the job on your behalf, if you're still looking for quotations from local tradesmen we suggest that you post a new job to increase your chances of fresh and competitive quotations. You can post a new job here: http://www.iwantatradesman.co.uk/post-a-job\r\n\r\nKind regards\r\n\r\nThe I Want A Tradesman Team\r\n\r\n\r\nThis email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.";

	$mail->Send();

// cancel job

$sql = "UPDATE jobs SET status = 'Closed' WHERE id = '".$rs['id']."'";
$cancel_job_query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());

// cancel quotes

$sql = "UPDATE quotes SET status = 'Cancelled' WHERE job_id = '".$rs['id']."'";
$cancel_quote_query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());

}

echo 'Done';

?>